#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pdfplumber
import pyttsx3

pdf = pdfplumber.open('../docs/navicat.pdf')

print('Total page:', len(pdf.pages))

current_page = pdf.pages[15]
print('current page:', current_page.page_number)

text = current_page.extract_text()
print(text)

content = text.replace('\n', '').replace('\r', '')

# 转化为语音

# 初始化来获取语音引擎
engine = pyttsx3.init()

# 调整人声类型， 支持0/1
voices = engine.getProperty('voices')
print('voices:', voices)
engine.setProperty('voices', voices[1].id)

# 调整语速， 范围0~500之间，默认200
rate = engine.getProperty('rate')
print('rate:', rate)
engine.setProperty('rate', 200)

# 调整声量， 范围0~1之间，默认1.0
volume = engine.getProperty('volume')
print('volume:', volume)
engine.setProperty('volume', 0.9)

# 朗读文本
engine.say(content)
# pyttsx3.speak(content)  # 单独执行
engine.runAndWait()
engine.stop()


#保存音频到本地
# engine.save_to_file(content, 'pdf.mp3')
# engine.runAndWait()
